
package com.example.home_helper;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class phone extends Fragment{
	String ui_style;
	String ip,port;
	String sendmessage;
	private Button btcd,btdj,btwd,bt4,bt5;
	private EditText edcd,eddj,edwd;
	private Socket socket = null;  
	private BufferedReader in = null;  
	private PrintWriter out = null;  
	private String content = "";  
	int level;
	String statusString="";
	int temperature;
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.phone, null);
		ui_style="*M_Phone#";
		SharedPreferences sharedPreferences =getActivity().getSharedPreferences("equipment",Context.MODE_PRIVATE);
		ip=sharedPreferences.getString("ip", "");
		port=sharedPreferences.getString("port", "");

		new Thread(){
			@Override
			public void run()
			{
				try {  
					InetAddress serverAddr = InetAddress.getByName ( ip );
					socket = new Socket(serverAddr, Integer.parseInt(port));  
					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
					out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(  
							socket.getOutputStream())), true);  
				} catch (IOException ex) {  
					ex.printStackTrace();  
				}  
				if (socket.isConnected()) {  
					if (!socket.isOutputShutdown()) {  
						out.println(ui_style);  
					}  
				}  
			}
		}.start();  
		btcd=(Button)v.findViewById(R.id.buttoncd);
		btdj=(Button)v.findViewById(R.id.buttondj);
		btwd=(Button)v.findViewById(R.id.buttonwd);
		bt4=(Button)v.findViewById(R.id.button4);
		bt5=(Button)v.findViewById(R.id.button5);
		edcd=(EditText)v.findViewById(R.id.editTextcd);
		eddj=(EditText)v.findViewById(R.id.editTextdj);
		edwd=(EditText)v.findViewById(R.id.editTextwd);
		btcd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(edcd.getText().toString()!=null){
					level=Integer.parseInt(edcd.getText().toString());
					sendmessage="*C"+String.valueOf(level)+"#";
					if (socket.isConnected()) {  
						if (!socket.isOutputShutdown()) {  
							out.println(sendmessage);  
						}  
					}  
				}
			}
		});
		btdj.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				level=Integer.parseInt(eddj.getText().toString());
				sendmessage="*D"+String.valueOf(level)+"#";
				if (socket.isConnected()) {  
					if (!socket.isOutputShutdown()) {  
						out.println(sendmessage);  
					}  
				}  
			}
		});
		btwd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				temperature=Integer.parseInt(edwd.getText().toString());
				sendmessage="*T"+String.valueOf(temperature)+"#";
				if (socket.isConnected()) {  
					if (!socket.isOutputShutdown()) {  
						out.println(sendmessage);  
					}  
				}  
			}
		});
		bt4.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendmessage="*D10#";
				if (socket.isConnected()) {  
					if (!socket.isOutputShutdown()) {  
						out.println(sendmessage);  
					}  
				}  
			}
		});
		bt5.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendmessage="*KS#";
				if (socket.isConnected()) {  
					if (!socket.isOutputShutdown()) {  
						out.println(sendmessage);  
					}  
				}  
			}
		});
		return v;
	}
}
